package menus.common;

import java.awt.Color;
import java.awt.geom.Point2D;
import java.util.ArrayList;

import fr.lri.swingstates.animations.Animation;
import fr.lri.swingstates.animations.AnimationTransparency;
import fr.lri.swingstates.canvas.CElement;
import fr.lri.swingstates.canvas.CShape;

/**
 * Skeleton for marking menu items
 * 
 * @author mathieu
 *
 */
public abstract class AbstractItem {
	
	/**
	 * Ghost color corresponding to a HIT
	 */
	protected static final Color COLOR_OK = Color.blue;
	
	/**
	 * Chost color corresponding to a MISS
	 */
	protected static final Color COLOR_WRONG = Color.red;
	
	/**
	 * Array containing every visual element composing the item
	 */
	protected CElement[] elements;
	
	/**
	 * The menu to which is attached this item
	 */
	protected AbstractMenu menu;
	
	/**
	 * The label of this item
	 */
	protected String label;
	
	/**
	 * The tag of this item
	 */
	protected String tag;
	
	
	
	// ----------------------------------------------------------------- XXX

	
	
	/**
	 * Default constructor
	 * @param menu
	 * @param label
	 */
	public AbstractItem(AbstractMenu menu, String label, String tag){
		
		this.menu = menu;
		this.label = label;
		this.tag = tag;
		
	}
	
	/**
	 * Generates and fades a clone of the specified element, at the specified location
	 * @param where
	 * @param ghost
	 */
	public void ghost(Point2D where, CShape ghost, boolean ok) {
		
		ghost.setOutlinePaint(ok?COLOR_OK:COLOR_WRONG).setTransparencyFill(1).setTransparencyOutline(1).setDrawable(true);
		
		menu.getCanvas().addShape(ghost);
		ghost.addTag(menu.toString());
		ghost.setDrawable(true);
		
		ghost.translateBy(where.getX(), where.getY());
		
		ghost.animate(createFadeOut());
		
	}
	
	/**
	 * Generates and fades a clone of the visual elements of this item away, at the specified location
	 * @param where
	 */
	public void ghost(Point2D where, boolean ok) {
		
		ghost(where, createShape(), ok);
		
	}
	
	/**
	 * Translates every visual element of this item by the specified coordinates
	 * @param dx
	 * @param dy
	 */
	public void translateBy(double dx, double dy){
		for (CElement e : elements){
			e.translateBy(dx, dy);
		}
	}
	
	/**
	 * Generates a fade out animation for the ghost() method
	 * @return
	 */
	public AnimationTransparency createFadeOut(){
		AnimationTransparency result = new AnimationTransparency(0){
			
			@Override
			public void doStop() {
				
				super.doStop();
				
				getAnimated().setTransparencyFill(1)
							.setTransparencyOutline(1)
							.setDrawable(false);
			}
		};
		
		result.setLapDuration(500);
		
		menu.addAnimation(result);
		
		return result;
	}
	
	/**
	 * @return
	 */
	public String getLabel(){
		return label;
	}
	
	
	
	// ----------------------------------------------------------------- XXX

	
	
	/**
	 * Creates and returns a shape corresponding to this item, without adding it to the menu or the canvas.
	 * TODO only one?...
	 * @return
	 */
	protected abstract CShape createShape();
	
}
